/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.kmagsoft.jconvert.gui;

import gr.anagnosg.general.utils.FileUtils;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.JTextField;

/**
 *
 * @author anagnosg
 */
public class KeyFileSelect {
    private javax.swing.JTextField textField;
    private String files[] ;
    private int listIndex = 0;
    private int tabHasBeenPressed=0; 
    private String valueSelected ;
    private String searchSourceStrTab;

    KeyFileSelect(JTextField JTextFieldp) {
        
         textField=JTextFieldp;
          textField.setFocusTraversalKeysEnabled(false);
    }
    
    
    
    public void resetSelect(){
        listIndex=0;
        tabHasBeenPressed=0;
        valueSelected = null;
    }
    
    public void keyPressed(java.awt.event.KeyEvent evt){
        if(evt.getKeyCode()==KeyEvent.VK_TAB){
           if(tabHasBeenPressed==0){
               searchSourceStrTab=this.textField.getText();
               listIndex=0;
           }
           files = FileUtils.findMatchingDirs(searchSourceStrTab);
           if(files!=null && files.length>0){
               String parentStr = this.textField.getText().substring(0,this.textField.getText().lastIndexOf(File.separatorChar)); 
               if( files.length>listIndex){
                    this.textField.setText(parentStr+File.separatorChar+files[this.listIndex]);
                    this.listIndex++;
                }
                else {
                    this.listIndex=0;
                    this.textField.setText(parentStr+File.separatorChar+files[this.listIndex]);
                }
                this.tabHasBeenPressed++;
           }
        evt.consume();
        }
        else{//reset
          resetSelect();
            
        }
    } 
    
}
